package com.attilax.ref;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.reflect.MethodUtils;

import com.attilax.compress.ZipUtil;
import com.attilax.util.ExUtil;
import com.google.common.collect.Lists;

public class refService {

	public static void main(String[] args)
			throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		String classfile = "C:\\0wkspc\\ftpserverati\\WebContent\\WEB-INF\\classes\\com\\attilax\\util\\clsNodep.class";
		classfile = "C:\\0wkspc\\ftpserverati\\WebContent\\WEB-INF\\classes";
		Class cls = refService.loadclassV2(classfile, "com.attilax.util.clsNodep");

		List<Object> paramsList = Lists.newArrayList();
		paramsList.add(null);

			System.out.println("--f");
	}

	/**
	 * @my hah
	 * @author attilax
	 * @since s530
	 * @param f
	 *            jar file or class file dir,,cant be single classfile path
	 * @param clsname
	 * @return cls
	 */
	public static Class loadclassV2(String f, String clsname) {

		try {
			Class c = null;
			File file = new File(f);
			if (!file.exists())
				throw new RuntimeException("file not exist:" + f);
			URL url = file.toURI().toURL();// 灏咶ile绫诲瀷杞负URL绫诲瀷锛宖ile涓簀ar鍖呰矾寰�
			// 寰楀埌绯荤粺绫诲姞杞藉櫒
			URLClassLoader urlClassLoader_getSystemClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
			// 鍥犱负URLClassLoader涓殑addURL鏂规硶鐨勬潈闄愪负protected鎵�互鍙兘閲囩敤鍙嶅皠鐨勬柟娉曡皟鐢╝ddURL鏂规硶
			Method method_addURL = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
			method_addURL.setAccessible(true);
			method_addURL.invoke(urlClassLoader_getSystemClassLoader, new Object[] { url });
			// urlClassLoader_getSystemClassLoader.
			c = Class.forName(clsname);
			return c;
		} catch (Exception e) {
			ExUtil.throwExV2(e);
		}
		// 鎴栬�
		// Class c=urlClassLoader.loadClass("绫诲悕");
		throw new RuntimeException("class load ex:" + clsname + ",f:" + f);

	}

	public static Class loadclass(String f, String clsname) {

		try {
			Class c = null;
			File file = new File(f);
			if (!file.exists())
				throw new RuntimeException("file not exist:" + f);
			URL url = file.toURI().toURL();// 灏咶ile绫诲瀷杞负URL绫诲瀷锛宖ile涓簀ar鍖呰矾寰�
			// 寰楀埌绯荤粺绫诲姞杞藉櫒
			URLClassLoader urlClassLoader_getSystemClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
			// 鍥犱负URLClassLoader涓殑addURL鏂规硶鐨勬潈闄愪负protected鎵�互鍙兘閲囩敤鍙嶅皠鐨勬柟娉曡皟鐢╝ddURL鏂规硶
			Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
			add.setAccessible(true);
			add.invoke(urlClassLoader_getSystemClassLoader, new Object[] { url });
			// urlClassLoader_getSystemClassLoader.
			c = Class.forName(clsname);
			return c;
		} catch (Exception e) {
			ExUtil.throwExV2(e);
		}
		// 鎴栬�
		// Class c=urlClassLoader.loadClass("绫诲悕");
		throw new RuntimeException("class load ex:" + clsname + ",f:" + f);

	}

	public static List<String> getClassName(String filePath) {
		if (!filePath.toLowerCase().endsWith(".jar"))
			if (!filePath.toLowerCase().endsWith(".zip"))
				throw new RuntimeException("file fmt err:" + filePath);
		List<String> clses = Lists.newArrayList();
		List<String> myClassName = ZipUtil.unzip_filelistV2(filePath);
		for (String f : myClassName) {
			if (!f.endsWith(".class"))
				continue;
			f = f.replaceAll("/", ".");
			int endDotpos = f.lastIndexOf(".");
			f = f.substring(0, endDotpos);
			clses.add(f);
		}

		return clses;
	}

}
